function addAdminInfo(){ 
    var o = document.admin;    
    var p = /^[\u4E00-\u9FA5\uf900-\ufa2d\w]{5,25}$/;
    var p2 = /^\s*[A-Za-z0-9_]{6,30}\s*$/;
    if(o.userName.value==''){
        alert('用户名不能空！');
        o.userName.focus();
        return;
    }else if(!p.test(o.userName.value)){
        alert('用户名只能用中文、英文、数字、下划线组成，[5-25]个字符！');
        o.userName.focus();
        return;
    }else if(o.password.value==''){
        alert('密码不能空！');
        o.password.focus();
        return;
    }else if(!p2.test(o.password.value)){
        alert('密码只能由数字字母和下划线组成，长度为[6-30]！');
        o.password.focus();
        return;
    }else if(o.password.value!=o.password2.value){
        alert('两次密码不一致！');
        o.password2.focus();
        return;
    }else if(o.safeMail.value!=""&&!checkEmail(o.safeMail.value)){
        alert("邮箱地址格式非法！");
        o.safeMail.focus();
        return;
    }else if(o.group.value==''){
        alert('请选择该管理员隶属哪个管理员组!');
        o.group.focus();
        return;
    }

    if(o.inherit.checked){
        // 继承改组权限
        manage.addAdmin(o.userName.value,o.password.value,o.safeMail.value,o.group.value,1,"",function(data){
            if(data==1){
                alert("管理员“"+o.userName.value+"”添加成功！");
                window.location.href='adminList.jsp';
            }else if(data==0){
                alert('未知原因，管理员添加失败！');
            }else if(data==-1){
                alert('管理员“'+o.userName.value+'”已经存在，添加失败！');
            }
        });
    }else{
        // 不继承该组管理权限
        var flag = false;
        var value="";
        for (var i=0;i<o.elements.length;i++) {
            var temp=o[i];
            if(temp.type=="checkbox"&&temp.checked&&temp.name=="second_menu") {
                flag = true;
                value += temp.value+",";
            }
        }

        if(!flag){
            if(confirm('该用户未继承组权限，您确认不为该用户赋予任何权限吗？')){
                manage.addAdmin(o.userName.value,o.password.value,o.safeMail.value,o.group.value,0,"",function(data){
                    if(data==1){
                        alert("管理员“"+o.userName.value+"”添加成功！");
                        window.location.href='adminList.jsp';
                    }else if(data==0){
                        alert('未知原因，管理员添加失败！');
                    }else if(data==-1){
                        alert('管理员“'+o.userName.value+'”已经存在，添加失败！');
                    }
                });
            }
        }else{
            manage.addAdmin(o.userName.value,o.password.value,o.safeMail.value,o.group.value,0,value,function(data){
                if(data==1){
                    alert("管理员“"+o.userName.value+"”添加成功！");
                    window.location.href='admin.jsp?type=add';
                }else if(data==0){
                    alert('未知原因，管理员添加失败！');
                }else if(data==-1){
                    alert('管理员“'+o.userName.value+'”已经存在，添加失败！');
                }else if(data==-2){
                    alert('管理员“'+o.userName.value+'”权限分配失败！');
                    window.location.href='admin.jsp?type=edit&name='+_E(o.userName.value);
                }else if(data==-3){
                    alert('管理员“'+o.userName.value+'”部分权限分配失败！');
                    window.location.href='admin.jsp?type=edit&name='+_E(o.userName.value);
                }
            });
        }
    }

}

function editAdminInfo(){
    var o = document.admin;
    var p = /^\s*[A-Za-z0-9_]{6,30}\s*$/;
    if(o.password.value==''){
        alert('密码不能空！');
        o.password.focus();
        return;
    }else if(o.password.value.length!=32&&!p.test(o.password.value)){
        alert('密码只能由数字字母和下划线组成，长度为[6-30]！');
        o.password.focus();
        return;
    }else if(o.password.value!=o.password2.value){
        alert('两次密码不一致！');
        o.password2.focus();
        return;
    }

    if(o.safeMail.value!=""){
        if(!checkEmail(o.safeMail.value)){
            alert("邮箱地址格式非法！");
            o.safeMail.focus();
            return;
        }
    }

    if(o.inherit.checked){
        // 继承改组权限
        manage.editAdmin(o.userName.value,o.password.value,o.safeMail.value,1,"",function(data){
            if(data==1){
                alert("管理员“"+o.userName.value+"”修改成功！");
                window.location.href='adminList.jsp';
            }else if(data==0){
                alert('未知原因，管理员修改失败！');
            }
        });
    }else{
        // 不继承该组管理权限
        var flag = false;
        var value="";
        for (var i=0;i<o.elements.length;i++) {
            var temp=o[i];
            if(temp.type=="checkbox"&&temp.checked&&temp.name=="second_menu") {
                flag = true;
                value += temp.value+",";
            }
        }

        if(!flag){
            if(confirm('您确认不为该用户赋予任何权限吗？')){
                manage.editAdmin(o.userName.value,o.password.value,o.safeMail.value,0,"",function(data){
                    if(data==1){
                        alert("管理员“"+o.userName.value+"”修改成功！");
                        window.location.href='adminList.jsp';
                    }else if(data==0){
                        alert('未知原因，管理员修改失败！');
                    }
                });
            }
        }else{
            manage.editAdmin(o.userName.value,o.password.value,o.safeMail.value,0,value,function(data){
                if(data==1){
                    alert("管理员“"+o.userName.value+"”修改成功！");
                    window.location.href='adminList.jsp';
                }else if(data==0){
                    alert('未知原因，管理员修改失败！');
                }else if(data==-1){
                    alert('管理员“'+o.userName.value+'”权限分配失败！');
                    window.location.href='admin.jsp?type=edit&name='+_E(o.userName.value);
                }else if(data==-2){
                    alert('管理员“'+o.userName.value+'”部分权限分配失败！');
                    window.location.href='admin.jsp?type=edit&name='+_E(o.userName.value);
                }
            });
        }
    }

}

function lockOrUnlockAdmin(name,flag){
    var msg="";
    if(flag==1){
        msg = "您确定要激活管理员“"+_D(name)+"”？他（她）可能设计到违规操作！";
    }else if(flag==0){
        msg="您确定要锁定管理员“"+_D(name)+"”？";
    }else{
        return;
    }

    if(confirm(msg)){
        manage.lockOrUnlockAdmin(name,flag,function(data){
            if(flag){
                if(data==1){
                    alert("激活管理员“"+_D(name)+"”成功！");
                    window.location.reload();
                }else{
                    alert("激活管理员“"+_D(name)+"”失败！");
                }
            }
            else{
                if(data==1){
                    alert("锁定管理员“"+_D(name)+"”成功！");
                    window.location.reload();
                }else{
                    alert("锁定管理员“"+_D(name)+"”失败！");
                }
            }
        });
    }
}

function deleteAdmin(name){
    if(name==""){
        alert("参数传递错误！");
    }
    else{
        manage.deleteAdmin(name,function(data){
            if(data==1){
                alert("成功删除管理员“"+_D(name)+"”\uff01");
                window.location.reload();
            }else{
                alert("\u5220\u9664\u7ba1\u7406\u5458\u201c"+_D(name)+"\u201d\u5931\u8d25\uff01");
            }
        });
    }
}
function setSafeMail(){
    var o = document.admin;
    var obj = $("save");
    if(o.safeMail.value==""){
        o.safeMail.focus();
        alert("安全邮箱不能空！");
        obj.removeAttribute("disabled");
    }else if(!checkEmail(o.safeMail.value)){
        o.safeMail.focus();
        alert("邮箱格式不正确，请重新输入！");
        obj.removeAttribute("disabled");
    }else{
        manage.setSafeMail(o.safeMail.value, function(data){
            if(data==1){
                alert("设置成功，您的安全邮箱是“"+o.safeMail.value+"”！");
                window.location.reload();
            }else{
                alert("未知原因，设置失败！");
                obj.removeAttribute("disabled");
            }
        });
    }
}
function applyCode(){
    manage.applyCode(function(data){
        if(data==1){
            alert("申请成功！请到您的邮件查收！");
            window.location.reload();
        }else{
            alert("申请失败！未知错误！");
        }
    });
}

function savePassword(){
    var o = document.admin;
    var p = /^\s*[A-Za-z0-9_]{6,30}\s*$/;
    if(o.code.value==""){
        o.code.focus();
        alert("请输入验证码！验证码已经发送到您的安全邮箱！");
    }else if(o.oldPassword.value==""){
        o.oldPassword.focus();
        alert("请输入原来的密码！");
    }else if(o.newPassword.value==""){
        o.newPassword.focus();
        alert("请输入新的密码！");
    }else if(!p.test(o.newPassword.value)){
        alert('密码只能由数字字母和下划线组成，长度为[6-30]！');
        o.newPassword.focus();
        return;
    }else if(o.newPassword.value!=o.repeatPassword.value){
        o.repeatPassword.focus();
        alert("两次输入的密码不一致！");
    }else{
        manage.checkCode(o.code.value, function(data){
            if(data==0){
                alert("未知错误，验证码检测失败！");
            }else if(data==-1){
                alert("验证码已经过期，请重新申请！");
            }else if(data==-2){
                o.code.focus();
                o.code.value="";
                alert("验证码错误！");
            }else{
                manage.saveAdminPassword(o.oldPassword.value,o.newPassword.value, function(d){
                    if(d==0||d==-1){
                        alert("未知错误，密码修改失败！");
                    }else if(d==-2){
                        alert("原始密码错误！");
                    }else {
                        alert("密码修改成功！");
                        parent.parent.window.location.href='logout.html';
                    }
                });
            }
        });
    }
}